package com.yylending.las.personal.loan;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yylending.las.YJKD;

import java.util.ArrayList;

import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * M:我的借款列表
 * Created by ying_he on 2017/6/5.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class LoanListModel extends Model {
    @Override
    public Observable getObservable(Object params, int functionalCode) {
        Service service = (Service) NetworkHelper.createService(Service.class);
        String token = YJKD.getInstance().getPreferenceManager().getLoginToken();
        return service.getLoanList(token, (EmptyEntity) params);
    }

    @Override
    public void onNext(Object entity) {
        LoanListResult loanListResult = cast(entity);
        EventAgent.post(loanListResult);
    }

    public interface Service {
        @POST("server/loan/getLoanRecord")
        Observable<LoanListResult> getLoanList(@Header("token") String token, @Body EmptyEntity requestParams);
    }

    static class LoanListResult extends BaseResultEntity<Loan> {

    }

    static class Loan {
        public String loanStatus;
        ArrayList<LoanDetail> detail = new ArrayList<>();

        public static class LoanDetail {
            /**
             * 借款时间
             */
            public String applyDate = "";
            /**
             * 借款总额
             */
            public String loanAmount = "";
            /**
             * 借款状态
             */
            public String loanStatus = "";
            /**
             * 借据号
             */
            public String loanNo = "";
            /**
             * 是否已逾期
             */
            public String isOverDue = "";

            /**
             * 是否可点击
             */
            public boolean isClickable(){
                boolean clickable = true;
                switch (loanStatus){
                    case LoanListActivity.LOAN_STATUS_CODE_NEGATIVE:
                    case LoanListActivity.LOAN_STATUS_CODE_EXPIRED:
                        clickable = false;
                        break;
                    default:
                        break;
                }
                return clickable;
            }
        }
    }

}
